package server;

import connect.ConnectionData;

import java.io.*;
import java.net.*;
import java.util.*;

public class ConnectionKeeper {
    private LinkedList clientList;
    private CommandParser cp;

    public ConnectionKeeper(CommandParser parser) {
        this.cp = parser;
        clientList = new LinkedList();
    }

    public void add(Socket s) {
        MainServer.connects++;
        clientList.addLast(new ConnectedClient(s, this));
    }

    public void remove(ConnectedClient cc) {
        clientList.remove(cc);
        cc = null;
    }

    public LinkedList users() {
        return clientList;
    }

    public void runCommand(ConnectedClient cc, ConnectionData con) {
        cp.runCommand(cc, con);
    }

    public void sendTo(ConnectedClient sender, String user, ConnectionData con) {
        boolean found = false;
        for (int i = 0; i < clientList.size(); i++) {
            ConnectedClient receiver = (ConnectedClient) (clientList.get(i));
            if (user.equalsIgnoreCase(receiver.nick)) {
                receiver.sendImage(con);
                found = true;
                i = clientList.size() + 5; // Stop the loop.
            }
        }
        if (!found) {
            sender.sendMessage("Unable to find user " + user);
        }
    }

    public void broadcast(ConnectionData con) {
        for (int i = 0; i < clientList.size(); i++) {
            ConnectedClient cc = (ConnectedClient) (clientList.get(i));
            cc.sendImage(con);
        }
    }
}
